<?php

namespace App;

use App\Ws;
// use App\Db;

class Open
{
    static public $server = null;
    public $request = null;
    public function __construct($server, $request)
    {
        self::$server = $server;
        $this->request = $request;
        $this->dispatch();
    }

    public function dispatch()
    {
        switch ($this->request->get['type']) {
            case "toon_pull":
                $this->storeUidOfClient();
                break;
            case "user_online":
                $this->storeFdOfAdm();
                break;
            case "admin_notice":
                $this->storeFdOfAdmNotice();
                break;
        }
    }

    public function storeUidOfClient()
    {
        $msg = [
            'fd' => $this->request->fd,
            'site' => empty($this->request->get['site']) ?  '' : $this->request->get['site'],
            'session' => empty($this->request->get['sessionId']) ? '' : $this->request->get['sessionId'],
            'from' => $this->request->header["origin"],
            'time' => time(),
        ];
        Ws::$table::get('uidTable')->set($this->request->fd, $msg);
        unset($msg);
    }

    public function storeFdOfAdm()
    {
        Ws::$table::get('admOnlineTable')->set($this->request->fd, ['fd' => $this->request->fd]);
    }

    public function storeFdOfAdmNotice()
    {
        Ws::$table::get('admTable')->set($this->request->fd, ['fd' => $this->request->fd]);
    }
}
